package fragment;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.HistoryEntity;

import java.util.ArrayList;
import java.util.List;

import dbdao.HistoryDBhelper;
import entity.PopularProgramEntity;
import tools.ItemOnClickListenerOther;
import utils.Common;
import utils.adapter.PopularProgramAdapter;
import utils.servicedown.MyDownReceiver;
import volley.VolleySingleton;

/**
 * 最火节目排行
 * Created by dllo on 15/11/19.
 */
public class PopularProgramPageFragment extends AbsBaseFragment implements ItemOnClickListenerOther {
    private RecyclerView recyclerView;
    private PopularProgramAdapter adapter;
    private ImageView titleForAllBack;
    private TextView titleForAllTv;
    MyDownReceiver receiver;

    private String URL = "http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/track?device=" +
            "android&key=ranking%3Atrack%3Aplayed%3A1%3A0&pageId=1&pageSize=40";
private String name="";
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_program_popular;

    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.popularProgramRView);
        titleForAllBack = findView(R.id.titleForAllBack);
        titleForAllTv = findView(R.id.titleForAllTv);
         receiver = new MyDownReceiver();
    }

    @Override
    public void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Common.ACTION_DOWNLOAD_PROGRESS);
        filter.addAction(Common.ACTION_DOWNLOAD_SUCCESS);
        filter.addAction(Common.ACTION_DOWNLOAD_FAIL);
        getContext().registerReceiver(receiver, filter);


    }

    //stop 的时候关闭广播
    @Override
    public void onStop() {
        super.onStop();
        getContext().unregisterReceiver(receiver);
    }

    @Override
    protected void initData() {
        adapter = new PopularProgramAdapter(getContext());
        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setItemOnClickListener(this);
        titleForAllBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        titleForAllTv.setText("最火节目飙升榜");
        initNetwork();

    }


    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                PopularProgramEntity.class, new Response.Listener<PopularProgramEntity>() {
                    @Override
                    public void onResponse(PopularProgramEntity response) {
                        if (response != null) {
                            List<PopularProgramEntity.ListEntity> list = response.getList();
                            adapter.addList(list);
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }


    int result;


    @Override
    public void itemListener(Object o, int i) {
        PopularProgramEntity.ListEntity pl = (PopularProgramEntity.ListEntity) o;

        switch (i) {
            case 0://页面跳转到playactivity
                goToPlay(pl.getTrackId());
                List<HistoryEntity> entities = new ArrayList<>();
                HistoryEntity entity = new HistoryEntity();
                entity.setAlbmid(pl.getAlbumId());
                entity.setTrackId(pl.getTrackId());
                entity.setNickname(pl.getNickname());
                entity.setPlayUrl(pl.getPlayPath32());
                entity.setSmallLogo(pl.getCoverSmall());
                entities.add(entity);
                HistoryDBhelper.getInstance().saveHost(entities);

                break;
            case 1:

                //下载功能
                Toast.makeText(getContext(), "加入下载列表", Toast.LENGTH_SHORT).show();
                PopularProgramEntity.ListEntity listEntity = (PopularProgramEntity.ListEntity) o;
                String path = ((PopularProgramEntity.ListEntity) o).getPlayPathAacv164();
                Log.d("PopularProgramPageFragm", path + "地址");
                String name = ((PopularProgramEntity.ListEntity) o).getAlbumTitle()+".mp3";
                if (DownloadService.getInstance() != null
                        && DownloadService.getInstance().getFlag() != Common.Flag_Init) {
                    Toast.makeText(getContext(), "已经在下载", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent it = new Intent(getContext(), DownloadService.class);
                it.putExtra("flag", "start");
                it.putExtra("url", path);
                it.putExtra("fileName", name);
                getContext().startService(it);

                break;
        }





    }


}
